//
//  LoginViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "ZhuCeViewController.h"
#import "User.h"
#import "DataBaseSingleton.h"
@interface LoginViewController ()

@property (nonatomic, retain)UILabel *alabel;
@property (nonatomic, retain)UILabel *blabel;
@property (nonatomic, retain)UITextField *aFeild;
@property (nonatomic, retain)UITextField *bFeild;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"用户登录";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *loginTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    loginTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:loginTableView];
    [loginTableView release];
    
    self.alabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 90, 100, 30)];
    self.alabel.backgroundColor = [UIColor whiteColor];
    self.alabel.text = @"用户名";
    [loginTableView addSubview:self.alabel];
    [self.alabel release];
    
    
    self.blabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 100, 30)];
    self.blabel.backgroundColor = [UIColor whiteColor];
    self.blabel.text = @"密码";
    [loginTableView addSubview:self.blabel];
    [self.blabel release];
    
    
    self.aFeild = [[UITextField alloc] initWithFrame:CGRectMake(180, 90, 120, 30)];
    self.aFeild.backgroundColor = [UIColor whiteColor];
    self.aFeild.borderStyle = 3;
    self.aFeild.placeholder = @"请输入用户名";
    [loginTableView addSubview:self.aFeild];
    [self.aFeild release];
    
    
    self.bFeild = [[UITextField alloc] initWithFrame:CGRectMake(180, 160, 120, 30)];
    self.bFeild.backgroundColor = [UIColor whiteColor];
    self.bFeild.borderStyle = 3;
    self.bFeild.placeholder = @"请输入密码";
    [loginTableView addSubview:self.bFeild];
    [self.bFeild release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 240, 100, 30);
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor grayColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginTableView addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(180, 240, 100, 30);
    [button1 setTitle:@"注册" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor redColor];
    [button1 addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginTableView addSubview:button1];
}

- (void)buttonAction:(UIButton *)btn
{
    NSMutableArray *userArray = [[DataBaseSingleton shareDataBase] selectUserName];
    NSMutableArray *passwdArray = [[DataBaseSingleton shareDataBase] selectUserPasswd];
    User *user = [[User alloc] init];
    user.user = self.aFeild.text;
    user.passwd = self.bFeild.text;

    if (![userArray containsObject:user.user]) {
        
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不存在" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertCon animated:YES completion:^{
            
        }];
        UIAlertAction *alertActCn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        [alertCon addAction:alertActCn];
    }
    else if ([userArray containsObject:user.user] && ![passwdArray containsObject:user.passwd])
    {
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertCon animated:YES completion:^{
            
        }];
        UIAlertAction *alertActCn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        [alertCon addAction:alertActCn];
    }else{
        
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"islogin"];
        [[NSUserDefaults standardUserDefaults] setObject:user.user forKey:@"name"];
        
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

- (void)loginAction:(UIButton *)gg
{
    ZhuCeViewController *zhuceVC = [[ZhuCeViewController alloc] init];
    [self.navigationController pushViewController:zhuceVC animated:YES];
    [zhuceVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
